#!/usr/bin/python3
# coding=utf-8
# Filename : UpdateDigkey.py
#Author:Bill Zhang
#Email:zchv@msn.com
#Digkey collection thread

from socket import timeout
import urllib.request
#import the BeautifulSoup which parse the html
from bs4 import BeautifulSoup
#from bs4.diagnose import diagnose
#import python's reguler expression lib
import re
class UpdateClass:
    """The UpdateClass:
        This class has the follow's method:
        1.getCode(self,url)
        2.toUpdate(self,HtmlCode,spplier)
    
        The detail of function is as follows:
            getCode:
                return the html code string
                or wrong code number when failed
            toUpdate:
                return the data list
    
    """
    def __init__(self):
        pass
    def getCode(self,url):    
        """The function getCode is to get the source code of the webpages"""
        
        user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
        headers = {'User-agent':user_agent,'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language':'q=0.8,en-US;q=0.6,en;q=0.4'}
        req = urllib.request.Request(url=url,headers=headers)
        #HtmlCode save the source code of the webpages
        response="no cache"
        try:
            response = urllib.request.urlopen(req,timeout=20)
            print(response)
        except urllib.error.HTTPError as e:   
            print('Error code:',e.code)
            return (666,e.code)
        except urllib.error.URLError as e:   
            print('Reason',e.reason) 
            return (666,e.reason)
        except timeout:
            return (408,0)
        HtmlCode=response.read().decode('utf-8') 
        self.HtmlCode= HtmlCode 
        return (0,0)
        ##parse the html by using the BeautifulSoup

        ##f = open(URL[-7:], 'w') # open for 'w'riting
        ##f.write(HtmlCode) # write text to file
        ##f.close() # close the file


if __name__=="__main__":
    """
    ---if __name=="__main__"-----
    This is for debug this moudle,
    When run this moudle as the main 
    the code below will run
    """
    url="http://www.digikey.cn/product-detail/zh/CMF653K0900FNEB11/CMF653K0900FNEB11-ND/3615003"
    for url in open('url.txt').readlines(): 
        a=UpdateClass()
        b=a.getCode(str(url))
        f=open("result.txt","a") 
        f.write("URL:%s\nResultCode:%s\n,Result:%s\n" % (str(url),str(b),a.HtmlCode)) 

        
